package com.ac.scum.log.web.model.server.controller;

import com.ac.common.core.exception.BaseException;
import com.ac.common.core.result.Result;
import com.ac.scum.log.bean.ServerAlarmConfig;
import com.ac.scum.log.bean.ServerSysConfig;
import com.ac.scum.log.web.model.server.mapper.ServerAlarmConfigMapper;
import com.ac.scum.log.web.model.server.mapper.ServerSysConfigMapper;
import com.ac.scum.log.web.utils.user.CurrentUserUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 服务器系统配置
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("server/config")
public class ServerConfigController {

    private final ServerSysConfigMapper serverSysConfigMapper;
    private final ServerAlarmConfigMapper serverAlarmConfigMapper;

    /**
     * 获取服务器系统配置
     */
    @GetMapping("system")
    public Result<ServerSysConfig> getServerSysConfig() throws BaseException {
        ServerSysConfig serverSysConfig = serverSysConfigMapper.selectOne(Wrappers.lambdaQuery(ServerSysConfig.class).eq(ServerSysConfig::getServerId, CurrentUserUtil.getCurrentServerId()));
        if (serverSysConfig == null) {
            serverSysConfig = new ServerSysConfig();
            serverSysConfig.setEnableAlarm(false);
            serverSysConfig.setServerId(CurrentUserUtil.getCurrentServerId());
            serverSysConfigMapper.insert(serverSysConfig);
        }
        return Result.ok(serverSysConfig);
    }

    /**
     * 获取服务器报警配置
     */
    @GetMapping("alarm")
    public Result<ServerAlarmConfig> getServerAlarmConfig() throws BaseException {
        ServerAlarmConfig serverAlarmConfig = serverAlarmConfigMapper.selectOne(Wrappers.lambdaQuery(ServerAlarmConfig.class).eq(ServerAlarmConfig::getServerId, CurrentUserUtil.getCurrentServerId()));
        if (serverAlarmConfig == null) {
            serverAlarmConfig = new ServerAlarmConfig();
            serverAlarmConfig.setServerId(CurrentUserUtil.getCurrentServerId());
            serverAlarmConfigMapper.insert(serverAlarmConfig);
        }
        return Result.ok(serverAlarmConfig);
    }

    /**
     * 设置服务器配置
     */
    @PostMapping("system")
    public Result<ServerSysConfig> setSystem(@RequestBody ServerSysConfig serverConfig) throws BaseException {
        serverSysConfigMapper.update(serverConfig, Wrappers.lambdaUpdate(ServerSysConfig.class).eq(ServerSysConfig::getServerId, CurrentUserUtil.getCurrentServerId()));
        return Result.ok(serverSysConfigMapper.selectOne(Wrappers.lambdaQuery(ServerSysConfig.class).eq(ServerSysConfig::getServerId, CurrentUserUtil.getCurrentServerId())));
    }

    /**
     * 设置报警配置
     */
    @PostMapping("alarm")
    public Result<ServerAlarmConfig> setAlarm(@RequestBody ServerAlarmConfig serverAlarmConfig) throws BaseException {
        serverAlarmConfigMapper.update(serverAlarmConfig, Wrappers.lambdaUpdate(ServerAlarmConfig.class).eq(ServerAlarmConfig::getServerId, CurrentUserUtil.getCurrentServerId()));
        return Result.ok(serverAlarmConfigMapper.selectOne(Wrappers.lambdaQuery(ServerAlarmConfig.class).eq(ServerAlarmConfig::getServerId, CurrentUserUtil.getCurrentServerId())));
    }

}
